package com.itheima.info.system.manager.controller;

import com.itheima.info.system.manager.domain.Student;
import com.itheima.info.system.manager.factory.Factory;
import com.itheima.info.system.manager.service.IBaseStudentService;


import java.util.ArrayList;
import java.util.Scanner;

public class StudentController implements IBaseStudentController {
    private Scanner sc = new Scanner(System.in);
    IBaseStudentService studentService = Factory.getStudentService();

    public void start() {
        while (true) {
            System.out.println("---------------------欢迎来到<学生>管理系统--------------------");
            System.out.println("\t1.添加学生\t2.删除学生\t3.修改学生\t4.查看学生\t5.退出系统");
            System.out.print("\t您的选择:");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    add();
                    break;
                case 2:
                    delete();
                    break;
                case 3:
                    updata();
                    break;
                case 4:
                    findAll();
                    break;
                case 5:
                    System.out.println("\t感谢使用");
                    return;
                default:
                    System.out.print("\t您的输入有误,请重新输入:");
                    break;
            }
        }
    }

    @Override
    public void add() {
        String sid;
        while (true) {
            System.out.print("\t请输入学号:");
            sid = sc.next();
            boolean exist = studentService.isExist(sid);
            if (exist) {
                System.out.println("\t学号已存在,请重新输入.");
            } else {
                break;
            }
        }

        Student student = getInfoStudent(sid);
        studentService.add(student);
        System.out.println("\t添加成功");
    }

    @Override
    public Student getInfoStudent(String sid) {
        System.out.print("\t请输入姓名:");
        String name = sc.next();
        System.out.print("\t请输入年龄:");
        int age = sc.nextInt();
        System.out.print("\t请输入班级:");
        String cls = sc.next();
        return new Student(name, sid, age, cls);
    }

    @Override
    public void delete() {
        if (studentService.isEmpty()) {
            System.out.println("\t没有数据,请先添加");
        } else {
            String sid = getSid();
            studentService.delete(sid);
            System.out.println("\t删除成功");
        }
    }

    @Override
    public String getSid() {
        String sid;
        while (true) {
            System.out.print("\t请输入学生的学号:");
            sid = sc.next();
            boolean exist = studentService.isExist(sid);
            if (exist) {
                break;
            } else {
                System.out.println("\t学号不存在,请重新输入.");
            }
        }
        return sid;
    }

    @Override
    public void updata() {
        if (studentService.isEmpty()) {
            System.out.println("\t没有数据,请先添加");
        } else {
            String sid = getSid();
            Student student = getInfoStudent(sid);
            studentService.updata(sid, student);
            System.out.println("\t修改成功");
        }
    }

    @Override
    public void findAll() {
        if (studentService.isEmpty()) {
            System.out.println("您还未录入数据,请先录入.");
        } else {
            ArrayList<Student> students = studentService.findAll();
            System.out.println("\t学号 \t姓名 \t年龄 \t班级");
            for (Student student : students) {
                System.out.println(student.getSid() + "\t" + student.getName() + "\t"
                        + student.getAge() + "\t" + student.getCls() + "\t");
            }
        }
    }

}
